package com.mpower.user.account;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.slayer.model.CreditCard;
import com.slayer.model.impl.CreditCardImpl;
import com.slayer.service.CreditCardLocalServiceUtil;


public class MyPaypalAccount extends MVCPortlet {

	public void saveAccountDetails(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		ArrayList<Long> myCardIdList=new ArrayList<Long>();
		int[] accountListIndexes = StringUtil.split(
				ParamUtil.getString(actionRequest, "ccAccountListIndexes"), 0);
		
		System.out.println("accountListIndexes"+accountListIndexes.length);
		
		for (int i = 0; i < accountListIndexes.length; i++) {

			int accListIndex = accountListIndexes[i];

			String cardType = ParamUtil.getString(actionRequest, "cardType"
					+ accListIndex);

			String cardFirstName = ParamUtil.getString(actionRequest,
					"cardFirstName" + accListIndex);

			String cardLastName = ParamUtil.getString(actionRequest,
					"cardLastName" + accListIndex);

			String cardNumber = ParamUtil.getString(actionRequest, "cardNumber"
					+ accListIndex);

			String cvv = ParamUtil.getString(actionRequest, "cvv"
					+ accListIndex);

			String expMM = ParamUtil.getString(actionRequest, "expMM"
					+ accListIndex);

			String expYY = ParamUtil.getString(actionRequest, "expYY"
					+ accListIndex);

			int defaultAccount = ParamUtil.getInteger(actionRequest,
					"defaultAccount");

			long myCardId = ParamUtil.getLong(actionRequest, "cardId"
					+ accListIndex);

			ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
					.getAttribute(WebKeys.THEME_DISPLAY);
			User user = themeDisplay.getUser();

			long userId = user.getUserId();
			
			myCardIdList.add(myCardId);
		
			
			if (myCardId > 0) {
				try {

					CreditCard myCCAccountObj = CreditCardLocalServiceUtil
							.getCreditCard(myCardId);

					myCCAccountObj.setUserId(userId);
					myCCAccountObj.setCardId(myCardId);
					//myCCAccountObj.setCardType(cardType);
					myCCAccountObj.setCardFirstName(cardFirstName);
					myCCAccountObj.setCardLastName(cardLastName);
					myCCAccountObj.setCardNumber(cardNumber);
					myCCAccountObj.setCardCVV(cvv);
					myCCAccountObj.setCardExpMonth(expMM);
					myCCAccountObj.setCardExpYear(expYY);
					myCCAccountObj.setDateModified(new Date());
					
										
					if (i == defaultAccount) {
						myCCAccountObj.setDefaultEntry(true);

					} else {
						myCCAccountObj.setDefaultEntry(false);
					}
					
					CreditCardLocalServiceUtil.updateCreditCard(myCCAccountObj);

				} catch (SystemException e) {
					e.printStackTrace();
				} catch (PortalException ee) {
					ee.printStackTrace();
				}
				} else {
				CreditCard myCCAccountObj = new CreditCardImpl();

				myCCAccountObj.setUserId(userId);
				myCCAccountObj.setCardId(myCardId);
				//myCCAccountObj.setCardType(cardType);
				myCCAccountObj.setCardFirstName(cardFirstName);
				myCCAccountObj.setCardLastName(cardLastName);
				myCCAccountObj.setCardNumber(cardNumber);
				myCCAccountObj.setCardCVV(cvv);
				myCCAccountObj.setCardExpMonth(expMM);
				myCCAccountObj.setCardExpYear(expYY);
				myCCAccountObj.setDateAdded(new Date());
				if (i == defaultAccount - 1) {
					myCCAccountObj.setDefaultEntry(true);
				} else {
					myCCAccountObj.setDefaultEntry(false);
				}
				try {
					CreditCardLocalServiceUtil.addCreditCard(myCCAccountObj);
					myCardIdList.add(myCCAccountObj.getCardId());
				} catch (SystemException e1) {
					e1.printStackTrace();
				}
			}
		}
	
		try {
			List<CreditCard> creditCards = CreditCardLocalServiceUtil.getCreditCards(0, 100);
			int totalCards = creditCards.size();
			
			for(CreditCard creditCard :creditCards){
				if(totalCards == 1){
					break;
				}
				if(!myCardIdList.contains(creditCard.getCardId())){
					CreditCardLocalServiceUtil.deleteCreditCard(creditCard);
					totalCards--;
				}
			}
		} catch (SystemException e) {
			e.printStackTrace();
		}
	}
}
